package zhang.automobileservice;

import android.content.Intent;
import android.graphics.Paint;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.toolbox.NetworkImageView;

import org.json.JSONException;
import org.json.JSONObject;

import bean.CollectBear;
import bean.Maintain_Combo_Item;
import utils.CollectDB;
import utils.MyUrl;
import utils.RequestQueueUtils;
import utils.ToastUtil;

public class MaintainDetailActivity extends FarterActivity implements View.OnClickListener {

    private NetworkImageView iv_pager;
    private ImageButton finish;
    private TextView tv_tit;
    private FrameLayout fl_01;
    private TextView tv_rank;
    private LinearLayout LL_collect;
    private LinearLayout LL_see;
    private LinearLayout LL_tel;
    private LinearLayout ll_01;
    private TextView tv_01;
    private TextView tv_price;
    private TextView tv_otherprice;
    private TextView tv_tab01;
    private TextView tv_tab02;
    private TextView tv_04;
    private TextView tv_00;
    private ImageView tv_km;
    private TextView tv_05;
    private ImageView tv_time;
    private ImageView tv_auto;
    private ImageView tv_wax;
    private TextView tv_000;
    private TextView tv_company;
    private TextView tv_companyaddress;
    private TextView tv_content;
    private String id;
    private Maintain_Combo_Item cmainDetail;
    private CollectDB db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maintain_detail);
        Intent intent = getIntent();
        id = intent.getStringExtra("id");
        initView();
        getData();
    }


    private void getData() {
        cmainDetail = new Maintain_Combo_Item();
        String url = MyUrl.Font_Url + MyUrl.queryDetail +"?id="+id;
        RequestQueueUtils.exectueJsonRequest(getApplicationContext(), url, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                try {
                    JSONObject object = jsonObject.getJSONObject("data");
                    cmainDetail.setId(object.getInt("id") + "");
                    cmainDetail.setContent(object.getString("content"));
                    cmainDetail.setPhone(object.getString("phone"));
                    cmainDetail.setPrice(object.getInt("price") + "");
                    cmainDetail.setPcount(object.getInt("pcount") + "");
                    cmainDetail.setImage(object.getString("image"));
                    cmainDetail.setName(object.getString("name"));
                    cmainDetail.setCom_detail(object.getString("com_detail"));
                    cmainDetail.setCom_location(object.getString("com_location"));
                    cmainDetail.setCompany(object.getString("company"));
                    cmainDetail.setOriginaPrice(object.getInt("originaPrice") + "");

                    bindData();
                } catch (JSONException e1) {
                    e1.printStackTrace();
                }
            }
        });
    }

    private void bindData() {
        String[] imgs = cmainDetail.getImage().split(",");
        String url = MyUrl.Font_Url + MyUrl.CarImage + imgs[0];
        RequestQueueUtils.exectueNetWorkImageView(getApplicationContext(), url, iv_pager);
        tv_01.setText(cmainDetail.getName().replaceAll("\n", " "));
        tv_price.setText("￥"+cmainDetail.getPrice() + " 万");
        tv_otherprice.setText("￥"+cmainDetail.getOriginaPrice() + " 万");
        tv_otherprice.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG );
        tv_tab01.setText("已预约" + cmainDetail.getPcount());
        tv_company.setText(cmainDetail.getCompany().replaceAll("\n", " "));
        tv_companyaddress.setText(cmainDetail.getCom_location());
        tv_content.setText(cmainDetail.getCom_detail());
    }

    private void initView() {
        iv_pager = (NetworkImageView) findViewById(R.id.iv_pager);
        finish = (ImageButton) findViewById(R.id.finish);
        tv_tit = (TextView) findViewById(R.id.tv_tit);
        fl_01 = (FrameLayout) findViewById(R.id.fl_01);
        tv_rank = (TextView) findViewById(R.id.tv_rank);
        LL_collect = (LinearLayout) findViewById(R.id.LL_collect);
        LL_see = (LinearLayout) findViewById(R.id.LL_see);
        LL_tel = (LinearLayout) findViewById(R.id.LL_tel);
        ll_01 = (LinearLayout) findViewById(R.id.ll_01);
        tv_01 = (TextView) findViewById(R.id.tv_01);
        tv_price = (TextView) findViewById(R.id.tv_price);
        tv_otherprice = (TextView) findViewById(R.id.tv_otherprice);
        tv_tab01 = (TextView) findViewById(R.id.tv_tab01);
        tv_tab02 = (TextView) findViewById(R.id.tv_tab02);
        tv_04 = (TextView) findViewById(R.id.tv_04);
        tv_00 = (TextView) findViewById(R.id.tv_00);
        tv_km = (ImageView) findViewById(R.id.tv_km);
        tv_05 = (TextView) findViewById(R.id.tv_05);
        tv_time = (ImageView) findViewById(R.id.tv_time);
        tv_auto = (ImageView) findViewById(R.id.tv_auto);
        tv_wax = (ImageView) findViewById(R.id.tv_wax);
        tv_000 = (TextView) findViewById(R.id.tv_000);
        tv_company = (TextView) findViewById(R.id.tv_company);
        tv_companyaddress = (TextView) findViewById(R.id.tv_companyaddress);
        tv_content = (TextView) findViewById(R.id.tv_content);
        finish.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.finish:
                finish();
                break;
            case R.id.LL_collect:
                CollectBear bear = new CollectBear();
                bear.setType("1");
                bear.setId(id);
                db = CollectDB.getInstance(getApplicationContext());
                db.saveCollect(bear);
                ToastUtil.show(getApplicationContext(), "收藏成功");
                break;
            case R.id.LL_tel:
                String tel = cmainDetail.getPhone();
                Intent intent2 = new Intent();
                intent2.setAction("android.intent.action.CALL");//指定动作名称，这个只在上面的Activity中找 Intent.ACTION_CALL;
                intent2.setData(Uri.parse("tel:" + tel));//传递号码，tel:实在上面Activity中定义的
                startActivity(intent2);
                break;
            case R.id.LL_see:
                View view = LayoutInflater.from(this).inflate(R.layout.pop_preset_watchcar, null);
                final PopupWindow popWindow1 = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
               /* ImageButton imageButton = (ImageButton) view.findViewById(R.id.ib_dismiss);
                imageButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        popWindow1.dismiss();
                    }
                });*/
                popWindow1.setBackgroundDrawable(new ColorDrawable(0xffffff));
                popWindow1.setFocusable(true);
                popWindow1.setOutsideTouchable(true);
                popWindow1.setTouchable(true);
                setAlpha(0.6f);
                popWindow1.showAtLocation(LL_see, Gravity.CENTER, 0, 0);
                popWindow1.setOnDismissListener(new PopupWindow.OnDismissListener() {
                    @Override
                    public void onDismiss() {
                        setAlpha(1);
                    }
                });

                break;
        }
    }
    public void setAlpha(float alpha) {
        Window window = getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.alpha = alpha;
        window.setAttributes(params);

    }
}
